﻿/*
	Date:			05/19/2012
	Author:         Fabien Olousouzian
	License:      	Microsoft Public License (Ms-PL)
	Product:        XNA Shader Composer - Project
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;

namespace Shader_Composer
{
    /// <summary>
    /// Abstraction of device
    /// </summary>
    public class Device
    {
        #region enum
        
        public enum MouseButton
        {
            LEFT,
            MIDDLE,
            RIGHT
        }

        #endregion

        #region Attributes

        private KeyboardState _currentKeyboardState;
        private KeyboardState _prevKeyboardState;

        private MouseState _currentMouseState;
        private MouseState _prevMouseState;

        private GamePadState _currentGamePadState;
        private GamePadState _prevGamePadState;

        #endregion

        #region Methods

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="type">Type of</param>
        public Device()
        {
            _currentKeyboardState = new KeyboardState();
            _currentGamePadState = new GamePadState();
            _currentMouseState = new MouseState();
        }

        /// <summary>
        /// Initialize the logic
        /// </summary>
        /// <param name="currentState"></param>
        public void Initialize(KeyboardState currentState)
        {
            _prevKeyboardState = _currentKeyboardState;
        }

        /// <summary>
        /// Initialize the logic
        /// </summary>
        /// <param name="currentState"></param>
        public void Initialize(GamePadState currentState)
        {
            _prevGamePadState = _currentGamePadState;
        }

        /// <summary>
        /// Initialize the logic
        /// </summary>
        /// <param name="currentState"></param>
        public void Initialize(MouseState currentState)
        {
            _prevMouseState = _currentMouseState;
        }

        /// <summary>
        /// Update the collection of events
        /// </summary>
        /// <param name="currentState"></param>
        public void Update(KeyboardState currentState)
        {
            _prevKeyboardState = _currentKeyboardState;
            _currentKeyboardState = currentState;
        }

        /// <summary>
        /// Update the collection of events
        /// </summary>
        /// <param name="currentState"></param>
        public void Update(MouseState currentState)
        {
            _prevMouseState = _currentMouseState;
            _currentMouseState = currentState;
        }

        /// <summary>
        /// Update the collection of events
        /// </summary>
        /// <param name="currentState"></param>
        public void Update(GamePadState currentState)
        {
            _prevGamePadState = _currentGamePadState;
            _currentGamePadState = currentState;
        }

        /// <summary>
        /// KeyJustPressed
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool keyJustPressed(Keys key)
        {
            return (_currentKeyboardState.IsKeyDown(key) && _prevKeyboardState.IsKeyUp(key));
        }

        /// <summary>
        /// buttonJustPressed
        /// </summary>
        /// <param name="button"></param>
        /// <returns></returns>
        public bool buttonJustPressed(Buttons button)
        {
            return (_currentGamePadState.IsButtonDown(button) && _prevGamePadState.IsButtonUp(button));
        }

        /// <summary>
        /// mouseJustPressed
        /// </summary>
        /// <param name="button"></param>
        /// <returns></returns>
        public bool mouseJustPressed(MouseButton button)
        {
            if (button == MouseButton.LEFT)
            {
                if (_currentMouseState.LeftButton == ButtonState.Released)
                    return true;
                else
                    return false;
            }

            if (button == MouseButton.MIDDLE)
            {
                if (_currentMouseState.MiddleButton == ButtonState.Released)
                    return true;
                else
                    return false;
            }

            if (button == MouseButton.RIGHT)
            {
                if (_currentMouseState.RightButton == ButtonState.Released)
                    return true;
                else
                    return false;
            }
            return false;
        }

        #endregion

        #region Getter & Setter

        /// <summary>
        /// Get the currentKeyboardState
        /// </summary>
        public KeyboardState CurrentKeyboardState
        {
            get { return _currentKeyboardState; }
        }

        /// <summary>
        /// Get the currentGamePadState
        /// </summary>
        public GamePadState CurrentGamePadState
        {
            get { return _currentGamePadState; }
        }

        /// <summary>
        /// Get the currentMouseState
        /// </summary>
        public MouseState CurrentMouseState
        {
            get { return _currentMouseState; }
        }

        #endregion

    }
}
